package com.easyadmin.modules.stock.controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easyadmin.common.aop.annotation.Repeat;
import com.easyadmin.common.web.base.BaseController;
import com.easyadmin.common.web.domain.request.PageDomain;
import com.easyadmin.common.web.domain.response.Result;
import com.easyadmin.common.web.domain.response.module.ResultTable;
import com.easyadmin.modules.stock.domain.StockDividendEntity;
import com.easyadmin.modules.stock.domain.SysFuturesEntity;
import com.easyadmin.modules.stock.domain.SysTradeDateEntity;
import com.easyadmin.modules.stock.mapper.SysTradeDateDao;
import com.easyadmin.modules.stock.service.SysTradeDateService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


/**
 * 每年交易日期表
 *
 * @author wanzenghui
 * @email lemon_wan@aliyun.com
 * @date 2025-04-03 20:54:57
 */
@RestController
@RequestMapping("/stock/date")
public class SysTradeDateController  extends BaseController {
    @Autowired
    private SysTradeDateService sysTradeDateService;
    @Autowired
    SysTradeDateDao sysTradeDateDao;


    /**
     * Describe: 基础路径
     */
    private static String MODULE_PATH = "stock/date/";

    /**
     * Describe: 获取角色列表视图
     * Param ModelAndView
     * Return 用户列表视图
     */
    @GetMapping("main")
    @ApiOperation(value = "获取列表视图")
    @PreAuthorize("hasPermission('/stock/date/main','stock:date:main')")
    public ModelAndView main() {
        return jumpPage(MODULE_PATH + "main");
    }


    /**
     * 分页查询
     */
    @ResponseBody
    @GetMapping("/list")
    @PreAuthorize("hasPermission('/stock/date/list','stock:date:list')")
    public ResultTable list(@ModelAttribute SysTradeDateEntity sysTradeDateEntity, PageDomain pageDomain) {
        PageInfo<SysTradeDateEntity> pageInfo = sysTradeDateService.queryPage(sysTradeDateEntity, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * Describe: 删除某条数据
     * Param: id
     * Return: Result
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除某条数据")
    @PreAuthorize("hasPermission('/stock/date/remove','stock:date:remove')")
    public Result remove(@PathVariable String id) {
        return decide(sysTradeDateService.remove(id));
    }


    /**
     * Describe: 获取修改视图
     * Param ModelAndView
     * Return 修改视图
     */
    @GetMapping("edit")
    @ApiOperation(value = "获取修改视图")
    @PreAuthorize("hasPermission('/stock/date/edit','stock:date:edit')")
    public ModelAndView edit(ModelAndView modelAndView, String id) {
        modelAndView.addObject("stockDate", sysTradeDateService.getById(id));
        modelAndView.setViewName(MODULE_PATH + "edit");
        return modelAndView;
    }


    /**
     * Describe: 修改角色信息
     * Param SysRole
     * Return 执行结果
     */
    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    @PreAuthorize("hasPermission('/stock/date/edit','stock:date:edit')")
    public Result update(@RequestBody SysTradeDateEntity sysDailyTopicThinkEntity) {
        return decide(sysTradeDateService.update(sysDailyTopicThinkEntity));
    }


    /**
     * Describe: 获取角色新增视图
     * Param ModelAndView
     * Return 角色新增视图
     */
    @GetMapping("add")
    @ApiOperation(value = "新增视图")
    @PreAuthorize("hasPermission('/stock/date/add','stock:date:add')")
    public ModelAndView add() {
        return jumpPage(MODULE_PATH + "add");
    }


    /**
     * Describe: 保存信息
     * Param SysRole
     * Return 执行结果
     */
    @Repeat
    @PostMapping("/save")
    @ApiOperation(value = "保存数据")
    @PreAuthorize("hasPermission('/stock/date/add','stock:date:add')")
    public Result save(@RequestBody SysTradeDateEntity sysTradeDateEntity) {
        List<SysTradeDateEntity> data =sysTradeDateDao.selectList(new QueryWrapper<SysTradeDateEntity>()
                .lambda().eq(StringUtils.isNotBlank(sysTradeDateEntity.getSysDate()),
                        SysTradeDateEntity::getSysDate,sysTradeDateEntity.getSysDate()));
        if(data.size()>0){
            return decide(false);
        }
        sysTradeDateEntity.setCreateTime(LocalDateTime.now());
        return decide(sysTradeDateService.save(sysTradeDateEntity));
    }


    /**
     * Describe: 批量删除接口
     * Param: ids
     * Return: Result
     */
    @DeleteMapping("batchRemove/{ids}")
    @ApiOperation(value = "批量删除数据")
    @PreAuthorize("hasPermission('/stock/warn/remove','stock:warn:remove')")
    public Result batchRemove(@PathVariable String ids) {
        return decide(sysTradeDateService.batchRemove(ids.split(",")));
    }
}
